package com.wang.controller.api;

import com.wang.dto.UserDto;
import com.wang.entity.User;
import com.wang.service.UserService;
import com.wang.utils.ConstantUtil;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Classname ApiUserController
 * @Description TODO
 * @Date 2025/3/20 11:48
 * @Created by pc
 */
@RestController
@RequestMapping("/api/user")
@Api(value = "前台用户接口",tags = "ApiUserController")
public class ApiUserController {
    @Resource
    private UserService userService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/reg")
    @ApiOperation(value = "用户注册",notes = "前台添加用户")
    @ApiImplicitParam(value = "用户DTO",name = "userDto",required = true,dataType = "UserDto")
    public ResponseUtil addUser(@RequestBody UserDto userDto){
        // 通过用户提交的参数中获取到 UUID 去redis 中查找是否存在  比对
        String phoneAndCode = stringRedisTemplate.opsForValue().get(userDto.getUuid());
        ResponseUtil r = null;
        if(phoneAndCode == null){
            // 证明redis中根本没有这个key
            r = ResponseUtil.get(ResponseEnum.SMS_VERIFY_4_CODE_NOT_IN_REDIS);
        }else{
            // 证明验证码有效 需要去比对 电话号码 和 code
            String phoneRedis = phoneAndCode.split("-")[0];
            String codeRedis = phoneAndCode.split("-")[1];
            if(userDto.getPhone().equals(phoneRedis) && userDto.getCode().equals(codeRedis)){
                // 比对正确  添加用户 并且 删除redis
                r = userService.apiAddUser(userDto);
                stringRedisTemplate.delete(userDto.getUuid());
            }else{
                // 比对错误
                r = ResponseUtil.get(ResponseEnum.SMS_VERIFY_4_CODE_NOT_MATCH);
            }
        }
        return r;
    }

    @PostMapping("/login")
    @ApiOperation(value = "登录",notes = "前台用户登录")
    @ApiImplicitParam(value = "用户实体",name = "user",required = true,dataType = "User")
    public ResponseUtil login(@RequestBody User user, HttpSession session){
        ResponseUtil responseUtil = userService.apiLogin(user);
        if(responseUtil.getCode().intValue() == ResponseEnum.USER_API_LOGIN_4.getCode()){
            session.setAttribute(ConstantUtil.LOGIN_USER,responseUtil.getData());
        }
        return responseUtil;
    }
    // 从session中获取当前用户
    @GetMapping("/getCurrentUser")
    @ApiOperation(value = "获取用户",notes = "前台从session中获取当前登录的用户")
    public ResponseUtil getCurrentUser(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER);
        return ResponseUtil.get(ResponseEnum.USER_API_GET_CURRENT_INFO,loginUser);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "注销用户",notes = "前台从session中移除当前登录的用户")
    public ResponseUtil logout(HttpSession session){
        session.invalidate();
        return ResponseUtil.get(ResponseEnum.USER_API_LOGOUT_SUCCESS);
    }

}
